/**
 * 存储管理工具类
 */

// 存储键名常量
export const STORAGE_KEYS = {
  VOICE_HISTORY: 'voice_history',
  AI_CHAT_HISTORY: 'ai_chat_history',
  AI_USER_QUICK: 'ai_user_quick',
  USER_PROFILE: 'user_profile',
  APP_SETTINGS: 'app_settings'
}

// 存储管理类
export class StorageManager {
  constructor() {
    this.maxHistorySize = 50
    this.maxQuickSize = 20
  }

  // 安全设置存储
  setItem(key, value) {
    try {
      const data = JSON.stringify(value)
      uni.setStorageSync(key, data)
      return true
    } catch (error) {
      console.error('存储失败:', error)
      return false
    }
  }

  // 安全获取存储
  getItem(key, defaultValue = null) {
    try {
      const data = uni.getStorageSync(key)
      return data ? JSON.parse(data) : defaultValue
    } catch (error) {
      console.error('读取存储失败:', error)
      return defaultValue
    }
  }

  // 删除存储
  removeItem(key) {
    try {
      uni.removeStorageSync(key)
      return true
    } catch (error) {
      console.error('删除存储失败:', error)
      return false
    }
  }

  // 清空所有存储
  clear() {
    try {
      uni.clearStorageSync()
      return true
    } catch (error) {
      console.error('清空存储失败:', error)
      return false
    }
  }

  // 获取存储信息
  getStorageInfo() {
    try {
      return uni.getStorageInfoSync()
    } catch (error) {
      console.error('获取存储信息失败:', error)
      return null
    }
  }

  // 语音历史记录管理
  saveVoiceHistory(history) {
    const limitedHistory = history.slice(0, this.maxHistorySize)
    return this.setItem(STORAGE_KEYS.VOICE_HISTORY, limitedHistory)
  }

  getVoiceHistory() {
    return this.getItem(STORAGE_KEYS.VOICE_HISTORY, [])
  }

  addVoiceHistoryItem(text) {
    const history = this.getVoiceHistory()
    const newItem = { text, time: Date.now() }
    const updatedHistory = [newItem, ...history].slice(0, this.maxHistorySize)
    return this.saveVoiceHistory(updatedHistory)
  }

  // AI聊天历史管理
  saveChatHistory(messages) {
    const limitedMessages = messages.slice(-this.maxHistorySize)
    return this.setItem(STORAGE_KEYS.AI_CHAT_HISTORY, limitedMessages)
  }

  getChatHistory() {
    return this.getItem(STORAGE_KEYS.AI_CHAT_HISTORY, [])
  }

  // 用户快捷指令管理
  saveUserQuick(quickList) {
    const limitedQuick = quickList.slice(0, this.maxQuickSize)
    return this.setItem(STORAGE_KEYS.AI_USER_QUICK, limitedQuick)
  }

  getUserQuick() {
    return this.getItem(STORAGE_KEYS.AI_USER_QUICK, [])
  }

  addUserQuickItem(item) {
    const quickList = this.getUserQuick()
    if (!quickList.includes(item)) {
      const updatedQuick = [...quickList, item].slice(0, this.maxQuickSize)
      return this.saveUserQuick(updatedQuick)
    }
    return false
  }

  removeUserQuickItem(item) {
    const quickList = this.getUserQuick()
    const updatedQuick = quickList.filter(i => i !== item)
    return this.saveUserQuick(updatedQuick)
  }

  // 用户资料管理
  saveUserProfile(profile) {
    return this.setItem(STORAGE_KEYS.USER_PROFILE, profile)
  }

  getUserProfile() {
    return this.getItem(STORAGE_KEYS.USER_PROFILE, {
      avatar: '/static/default-avatar.svg',
      name: '用户',
      school: '',
      emergencyPhone: ''
    })
  }

  // 应用设置管理
  saveAppSettings(settings) {
    return this.setItem(STORAGE_KEYS.APP_SETTINGS, settings)
  }

  getAppSettings() {
    return this.getItem(STORAGE_KEYS.APP_SETTINGS, {
      voiceEnabled: true,
      notificationsEnabled: true,
      theme: 'light'
    })
  }

  // 清理过期数据
  cleanExpiredData() {
    const storageInfo = this.getStorageInfo()
    if (storageInfo && storageInfo.currentSize > 5 * 1024 * 1024) { // 5MB
      // 清理旧的聊天历史
      const chatHistory = this.getChatHistory()
      if (chatHistory.length > 20) {
        this.saveChatHistory(chatHistory.slice(-20))
      }
      
      // 清理旧的语音历史
      const voiceHistory = this.getVoiceHistory()
      if (voiceHistory.length > 30) {
        this.saveVoiceHistory(voiceHistory.slice(0, 30))
      }
    }
  }
}

// 创建全局存储管理器实例
export const storageManager = new StorageManager()
